#ifndef CUDPTRANSPORT_H_
#define CUDPTRANSPORT_H_

#include "IBaseDatalink.h"
#include "CDatalinkUtil.h"
#include <mutex>

namespace zl
{
namespace zlbase
{

/// \brief UDP链路
class CUDPDatalink : public IBaseDatalink
{
public:
    CUDPDatalink();
    ~CUDPDatalink();

    inline std::string Origin() override
    {
        return generate_datalink_origin(option_);
    }

    inline void AcquireOwnership(bool flag) override
    {
        flag ? mutex_.lock() : mutex_.unlock();
    }

    bool IsOpen() override;

    int32_t Open() override;

    int32_t Close() override;

    int32_t Read(unsigned char *buff, size_t len) override;

    int32_t Write(const unsigned char *buff, size_t len) override;

protected:
    int32_t sync_read(unsigned char *buff, size_t len, int32_t timeout);
    int32_t sync_write(const unsigned char *buff, size_t len, int32_t timeout);

private:
    struct DatalinkImpl;
    DatalinkImpl *datalink_;
    std::mutex mutex_;
};

} // namespace zlbase
} // namespace zl

#endif // CUDPTRANSPORT_H_